package data;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import android.app.Activity;

/**
 * This class checks if the account exists
 * 
 * @author Pooja
 */
public class AccountExists extends DBBooleanTask<Account> {

	/**
	 * Takes in an activity and and account to create the account
	 * 
	 * @param activity
	 *            - the activity for loading bar
	 * @param acct
	 *            - the account to check if exists
	 * @throws DBInterfaceError
	 */
	public AccountExists(Activity activity, Account acct)
			throws DBInterfaceError {
		super(activity, acct);

		System.out.println("got here");
	}

	/**
	 * This tasks actually checks if the account exists
	 * 
	 * @param account
	 *            - the account to check
	 * @return if the account is successfully added
	 */
	@Override
	protected boolean task(Account account) {
		boolean exists = false;

		System.out.println("apple panda");
		PreparedStatement accountExistsQuery;
		try {
			accountExistsQuery = conn
					.prepareStatement("SELECT * FROM account WHERE accountName = ?");
			accountExistsQuery.setString(1, account.getAccountName());
			ResultSet accounts = accountExistsQuery.executeQuery();
			exists = accounts.next();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return exists;
	}

}
